<?php



class jsonRPCServer {

	public static function handle($object){

		if($_SERVER['REQUEST_METHOD'] != 'POST' || empty($_SERVER['CONTENT_TYPE']) || $_SERVER["CONTENT_TYPE"] != 'application/json'){

			return false;
		}

		$request = json_decode(file_get_contents('php://input'),true);

		try{
			 if ($result = @call_user_func_array(array($object,$request['method']),$request['params'])) {
			 	
                $response = array ( 'id'=> $request['id'],'result'=> $result,'error'=> NULL );
            } else {
                $response = array ( 'id'=> $request['id'], 'result'=> NULL,
                                        'error' => 'unknown method or incorrect parameters' );}
		}catch(Exception $e){

			 $response = array ('id' => $request['id'],'result' => NULL, 'error' =>$e->getMessage());
		}

		if (!empty($request['id'])) { // notifications don't want response
            header('content-type: text/javascript');
            echo json_encode($response);
        }
        return true;
	}
}

class member{
    public function getName(){
        return 'hello word ' ;  // 返回字符串
    }
}


$myExample = new member();
// 注入实例
jsonRPCServer::handle($myExample) or print 'no request';